window._url = 'http://api.1hello.cn/pc-gateway-api/v1/';     // API地址
window._url2 = 'http://api.1hello.cn/pc-gateway-api/';
window.uploadUrl = 'http://file.1hello.cn/wbuild-file/v1/resource/_upload';   // 文件服务器地址
window.exclUrl = 'http://file.1hello.cn/wbuild-file/v1/';    //导出账单费项地址
window.convert2pdfApi = 'http://file.1hello.cn/wbuild-file/v1/resource/convert2pdf'

var hlhttp = (function (w) {
	var userInfo = w.localStorage.getItem('userMes') ? JSON.parse(w.localStorage.getItem('userMes')) : {}

  var conf = {
    url:_url,
    headers: { token:  Cookies.get("token") },
    data: {},
    error: function (xhr, type) {
      var res = JSON.parse(xhr.responseText)
			hl.toast(res.msg)
			// 如果为 德必用户登录 token失效时  不跳到登录页面
			if (res.code === 'USER_NOT_LOGIN') {
				if (getQueryString('corp_type') == 'SINOOCEAN') {
					$.ajax({
					  url: _url + 'corpapi/sinoocean/sso_passport_url',
					  contentType:'application/x-www-form-urlencoded',
					  type: 'get',
					  headers: {},
					  data: {
							tc: encodeURIComponent(getQueryString('tc')),
							corp_type: getQueryString('corp_type'),
							url: window.location.href
						},
					  success: function (data) {
					    w.location.href = data.ssoPassportUrl
					  }
					})
					return
				}
				if (userInfo.preSysCode == 'DEBI') return false
				w.location.href = 'login.html'
			}
    }
  }

  var http = {
    get: function (options) {
      var link = options.baseUrl || _url;
      $.ajax({
        url: link + options.url,
        contentType:'application/x-www-form-urlencoded',
        type: 'get',
        headers: options.headers || conf.headers,
        data: options.data || conf.data,
        success: function (data) {
          if (options.success)options.success(data)
        },
        error: options.error || conf.error
      })
    },
    post: function (options) {
      var link = options.baseUrl || _url;
      $.ajax({
        url: link + options.url,
        contentType: options.contentType || 'application/x-www-form-urlencoded',
        type: 'post',
        headers: options.headers || conf.headers,
        data: options.data || conf.data,
        success: function (data) {
          if (options.success)options.success(data)
        },
        error: options.error || conf.error
      })
    },
    put: function (options) {
      var link = options.baseUrl || _url;
      $.ajax({
        url: link + options.url,
        contentType:'application/x-www-form-urlencoded',
        type: 'put',
        headers: options.headers || conf.headers,
        data: options.data || conf.data,
        success: function (data) {
          if (options.success)options.success(data)
        },
        error: options.error || conf.error
      })
    },
    delete: function (options) {
      var link = options.baseUrl || _url;
      $.ajax({
        url: link + options.url,
        contentType:'application/x-www-form-urlencoded',
        type: 'delete',
        headers: options.headers || conf.headers,
        data: options.data || conf.data,
        success: function (data) {
          if (options.success)options.success(data)
        },
        error: options.error || conf.error
      })
    }
  }
  /**
   * @param {string} option.url 请求地址
   * @param {string=} option.headers 请求头
   * @param {string=} option.data 请求体
   * @param {string=} option.success 请求成功回调
   * @param {string=} option.error 请求失败回调
   */
  return {
    get: function (options) {
      return http.get(options)
    },
    post: function (options) {
      return http.post(options)
    },
    put: function (options) {
      return http.put(options)
    },
    delete: function (options) {
      return http.delete(options)
    }
  }

	function getQueryString(name) {
	  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	  var r = window.location.href.substr(window.location.href.indexOf('?') + 1).match(reg);
	  if (r != null) {
	    return unescape(r[2]);
	  }
	  return null;
	}

}(window))
